package com.dingding.gmall.order.listener;

import com.dingding.gmall.order.entity.OrderEntity;
import com.dingding.gmall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author liugongding
 * @description:
 * @date 2022/3/29 11:48 PM
 */

@Slf4j
@Component
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void handleCloseOrder(OrderEntity entity, Channel channel, Message message) throws IOException {
        log.info("收到订单过期消息");
        try {
            orderService.closeOrder(entity);
            //可以手动收单
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
